class qcm
{
public:
  qcm(const image2d<value::rgb8>& tmpl)
    : tmpl_ (tmpl)
  {
    jour[0] = 0;
    jour[1] = 0;
    mois = 0;
    an[0] = 0;
    an[1] = 0;
    s[0] = 0;
    s[1] = 0;
    m[0] = 0;
    m[1] = 0;
    m[2] = 0;

    for (int i = 0; i < 8; ++i)
      login[i] = '\0';
    
    for (int i = 0; i < 51; ++i)
      for (int j = 0; j < 5; ++j)
	{
	  questions[i][0][j] = false;
	  questions[i][1][j] = false;
	}

    for (int i = 0; i < 6; ++i)
      question51[i] = -1;
    for (int i = 0; i < 3; ++i)
      {
	questions2[0][i] = -1;
	questions2[1][i] = -1;
      }
    for (int i = 0; i < 3; ++i)
      questions3[i] = '\0';
  }

  void take(const box2d& b)
  {
    value::rgb8 v;
    v = tmpl_(b.center());
    
    if ((v.red() >= 1) && (v.red() <= 50))
      questions[v.red()][v.blue()][v.green() - 'A'] = true;

    if ((v.red() == 60) || (v.red() == 61))
      jour[v.red() - 60] += v.green();

    if (v.red() == 63)
      mois += v.green() + 1;

    if ((v.red() == 64) || (v.red() == 65))
      an[v.red() - 64] += v.green();

    if ((v.red() == 66) || (v.red() == 67))
      s[v.red() - 66] += v.green();

    if ((v.red() >= 68) && (v.red() <= 70))
      m[v.red() - 68] += v.green();

    if ((v.red() >= 71) && (v.red() <= 77))
      login[v.red() - 71] = v.green();

    if (v.red() == 51)
      question51[v.blue()] = v.green();

    if ((v.red() == 52) || (v.red() == 53))
      questions2[v.red() - 52][v.blue()] = v.green();

    if ((v.red() >= 54) && (v.red() <= 56))
      questions3[v.red() - 54] = v.green();
  }

  friend std::ostream& operator<<(std::ostream& out,
				  const qcm& q);

private:
  unsigned jour[2];
  unsigned mois;
  unsigned an[2];
  unsigned s[2];
  unsigned m[3];
  char login[7];
  bool questions[51][2][5];
  int question51[6];
  int questions2[2][3];
  char questions3[3];

  const image2d<value::rgb8>& tmpl_;
};

std::ostream& operator<<(std::ostream& out,
			 const qcm& q)
{
  out << "JOUR " << q.jour[0] << q.jour[1] << std::endl;
  out << "MOIS " << q.mois << std::endl;
  out << "AN " << q.an[0] << q.an[1] << std::endl;
  out << "S " << q.s[0] << q.s[1] << std::endl;
  out << "M " << q.m[0] << q.m[1] << q.m[2] << std::endl;

  out << "LOGIN ";
  int i = 0;
  while ((q.login[i] != '\0') && (i < 6))
    {
      out << q.login[i];
      ++i;
    }
  out << '_';
  if (q.login[6] != '\0')
    out << q.login[6];
  out << std::endl;

  for (int i = 1; i <= 50; ++i)
    {
      int row = 1;

      for (int j = 0; j < 5; ++j)
	row = q.questions[i][0][j] ? row : 0;

      out << i << " ";

      for (char l = 'A'; l <= 'E'; ++l)
	if (q.questions[i][row][l - 'A'])
	  out << l;

      out << std::endl;
    }

  out << "51 ";
  for (int i = 0; i < 6; ++i)
    {
      if (q.question51[i] != -1)
	out << q.question51[i];
    }
  out << std::endl;

  for (int j = 0; j <= 1; ++j)
    {
      out << j + 52 << " ";

      for (int i = 0; i < 3; ++i)
	{
	  if (q.questions2[j][i] != -1)
	    out << q.questions2[j][i];
	}

      out << std::endl;
    }

  for (int j = 0; j < 3; ++j)
    {
      out << j + 54 << " ";

      if (q.questions3[j] != '\0')
	out << q.questions3[j];

      out << std::endl;
    }
  return out;
}
